package ai.leinao.notify.core.template;

import cn.hutool.core.bean.BeanUtil;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 *
 *
 * @author zhong.wang
 * @date 2025/7/8
 */
public abstract class AbstractTemplate<Self extends AbstractTemplate<Self>> implements Template {
    /**
     * 模板变量
     */
    protected Map<String, VariableDefinition> variables;

    /**
     * 转换属性
     * @param properties
     * @return
     */
    public Self of(TemplateProperties properties) {
        if (this.variables == null) {
            this.variables = new LinkedHashMap<>();
        }
        BeanUtil.copyProperties(properties.getTemplate(), this);
        //动态配置的变量
        if (null != properties.getVariableDefinitions()) {
            for (VariableDefinition embeddedVariable : properties.getVariableDefinitions()) {
                this.variables.put(embeddedVariable.getId(), embeddedVariable);
            }
        }
        return toSelf();
    }

    /**
     * 转换自身
     * @return
     */
    protected Self toSelf() {
        return (Self) this;
    }
}
